package com.dfire;

import java.io.*;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;


/**
 * @author <a href="mailto:huoguo@2dfire.com">火锅</a>
 * @time 18/7/2
 */
public class FileDownLoad {

    // 扫描的文件目录
    private static String dirPath = "hdfs://hadoop1006:8020/zeus/hdfs-upload-dir";
    // 本地目录
    private static String downloadDir = "/Users/qqr/Downloads/hdfsTmp/";

    public static void main(String[] args) throws IOException {
        if(args.length != 0){
            dirPath = args[0];
            downloadDir = args[1];
        }
        System.out.println(dirPath+":"+downloadDir);
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(URI.create(dirPath), conf);
        List<Path> pathList = listDirFile(fs);
        downloadFile(fs, pathList);

    }


    /**
     * 下载文件
     *
     * @param fs
     * @param list
     * @throws IOException
     */
    private static void downloadFile(FileSystem fs, List<Path> list) throws IOException {
        for (Path path : list) {
            InputStream in = fs.open(path);
            File file = new File(downloadDir + path.getName());
            if (file.exists()) {
                file.delete();
            }
            file.createNewFile();
            OutputStream out = new FileOutputStream(file.getPath());
            IOUtils.copyBytes(in, out, 4096, true);
            System.out.println("下载完成:"+path);
        }

    }

    /**
     * 列出文件信息
     *
     * @param fs
     * @return
     * @throws IOException
     */
    private static List<Path> listDirFile(FileSystem fs) throws IOException {
        RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path(dirPath), true);
        List<Path> list = new ArrayList<Path>();
        while (listFiles.hasNext()) {
            LocatedFileStatus file = listFiles.next();
            System.out.println("path:" + file.getPath());
            list.add(file.getPath());
        }
        return list;
    }

}